/* ===================

AX2E (AXESCON XACML 2.0 Engine) is the Java authorization engine, which implements OASIS XACML 2.0 standard.
Copyright (C) 2007 AXESCON LLC

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA


Contact: AXESCON LLC - info{at}axescon.com

 =================== */
package axescon.xacml.xb.ctx;

import axescon.xacml.model.ctx.*;
import axescon.xacml.model.ctx.impl.*;
import axescon.xacml.api.RequestFactory;
import x0ContextSchemaOs.oasisNamesTcXacml2.*;

import java.io.Reader;

/**
 * @author argyn
 *         Date: Sep 19, 2005
 *         Responsibilities: load RequestFactory from XML and save it to XML
 *         using XMLBeans
 */
public class XBRequestFactory implements RequestFactory {
    public Request getRequest(Reader r) throws Exception {
        return getRequest(RequestDocument.Factory.parse(r).getRequest());
    }

    private Request getRequest(RequestType xbrt) throws Exception {
        RequestImpl ret = new RequestImpl();

        ret.setSubjectArray(getSubjectList(xbrt));
        ret.setResourceArray(getResourceList(xbrt));
        ret.setAction(getAction(xbrt.getAction()));
        ret.setEnvironment(getEnvironment(xbrt.getEnvironment()));
        ret.setLLObject(xbrt);

        return ret;
    }

    private Subject[] getSubjectList(RequestType xbrt) throws Exception {
        if (xbrt.sizeOfSubjectArray() == 0) return null;

        Subject[] ret = new Subject[xbrt.sizeOfSubjectArray()];
        for (int i = 0; i < xbrt.sizeOfSubjectArray(); i++) {
            SubjectType st = xbrt.getSubjectArray(i);
            ret[i] = getSubject(st);
        }
        return ret;
    }

    private Resource[] getResourceList(RequestType xbrt) throws Exception {
        if (xbrt.sizeOfResourceArray() == 0) return null;

        Resource[] ret = new Resource[xbrt.sizeOfResourceArray()];
        for (int i = 0; i < xbrt.sizeOfResourceArray(); i++) {
            ResourceType st = xbrt.getResourceArray(i);
            ret[i] = getResource(st);
        }
        return ret;
    }


    public Subject getSubject(SubjectType xbst) throws Exception {
        if (xbst == null) return null;

        Subject ret = new SubjectImpl();
        if (xbst.sizeOfAttributeArray() > 0) ret.setAttributeArray(getAttributeArray(xbst.getAttributeArray()));
        ret.setSubjectCategory(xbst.getSubjectCategory());
        return ret;
    }

    public RequestPart getEnvironment(EnvironmentType xbet) throws Exception {
        RequestPart ret = new EnvironmentImpl();
        if (xbet != null && xbet.sizeOfAttributeArray() > 0)
            ret.setAttributeArray(getAttributeArray(xbet.getAttributeArray()));

        return ret;
    }

    public Resource getResource(ResourceType xbrt) throws Exception {
        if (xbrt == null) return null;

        Resource ret = new ResourceImpl();
        if (xbrt.sizeOfAttributeArray() > 0) ret.setAttributeArray(getAttributeArray(xbrt.getAttributeArray()));
        ret.setResourceContent(getResourceContent(xbrt.getResourceContent()));
        return ret;
    }

    public RequestPart getAction(ActionType xbat) throws Exception {
        if (xbat == null) return null;

        RequestPart ret = new RequestPartImpl();

        if (xbat.sizeOfAttributeArray() > 0) ret.setAttributeArray(getAttributeArray(xbat.getAttributeArray()));
        return ret;
    }

    private Attribute[] getAttributeArray(AttributeType[] atl) throws Exception {
        Attribute[] ret = new Attribute[atl.length];
        for (int i = 0; i < atl.length; i++) {
            AttributeType at = atl[i];
            ret[i] = (getAttribute(at));
        }
        return ret;
    }

    private Attribute getAttribute(AttributeType xbat) throws Exception {
        Attribute ret = new AttributeImpl();
        ret.setIssuer(xbat.getIssuer());
        ret.setDataType(xbat.getDataType());
        ret.setAttributeId(xbat.getAttributeId());

        ret.setAttributeValueArray(getAttributeValueArray(xbat.getAttributeValueArray(), xbat.getDataType()));
        return ret;
    }

    private AttributeValue[] getAttributeValueArray(AttributeValueType[] avtl, String dataType) throws Exception {
        AttributeValue[] ret = new AttributeValue[avtl.length];
        for (int i = 0; i < avtl.length; i++) {
            AttributeValueType avt = avtl[i];
            ret[i] = (getXbAttrValueFactoryManager().getAttributeValue(avt, dataType));
        }
        return ret;
    }

    private ResourceContent getResourceContent(ResourceContentType xbrct) throws Exception {
        return null;
    }

    private XBAttrValueFactoryManager xbAttrValueFactoryManager = new XBAttrValueFactoryManager();

    public XBAttrValueFactoryManager getXbAttrValueFactoryManager() {
        return xbAttrValueFactoryManager;
    }

    public void setXbAttrValueFactoryManager(XBAttrValueFactoryManager xbAttributeValueFactoryManager) {
        this.xbAttrValueFactoryManager = xbAttributeValueFactoryManager;
    }

    public XBRequestFactory(XBAttrValueFactoryManager xbAttributeValueFactoryManager) {
        this.xbAttrValueFactoryManager = xbAttributeValueFactoryManager;
    }

    public XBRequestFactory(boolean setDefaults) {
        if (setDefaults) setDefaults();
    }

    public XBRequestFactory() {
        this(false);
    }

    public void setDefaults() {
        setXbAttrValueFactoryManager(new XBAttrValueFactoryManager(true));
    }

}
